﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using KeRui.Model;
using System.Collections.Generic;

public partial class Manage_Cms_CatchItem : System.Web.UI.Page
{
    protected KeRui.BLL.CmsCatch bllCmsCatch = new KeRui.BLL.CmsCatch();
    protected KeRui.Model.CmsCatch modelCmsCatch = new CmsCatch();

    protected KeRui.BLL.CmsCatchParse bllCmsCatchParse = new KeRui.BLL.CmsCatchParse();
    protected KeRui.Model.CmsCatchParse modelCmsCatchParse = new CmsCatchParse();
    public string Act = ManageHelper.PageAct();
    private ManageErrMsg nsb = new ManageErrMsg();
    protected string ActFlag = "addsave";
    protected string ActName = "添加";
    private int ServerID = -1;
    protected KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();

    protected KeRui.Model.CmsSite modelCmsSite = new CmsSite();
    protected KeRui.BLL.CmsSite bllCmsSite = new KeRui.BLL.CmsSite();

    protected void Page_Load(object sender, EventArgs e)
    {
        //登录
        ManageHelper.CheckAdminLogin();
        ManageHelper.CheckAdminPower("system_catchview");
        KeRui.BLL.CmsServer bllCmsServer = new KeRui.BLL.CmsServer();
        ServerID = bllCmsServer.GetServerIDByIP(PageRequest.GetServerIP());
        ManageHelper.CheckAdminLogin();
        switch (ManageHelper.PageAct())
        {
            case "addsave":
                Add();
                break;
            case "edit":
                Edit();
                break;
            case "editsave":
                EditSave();
                break;
            case "del":
                Del();
                break;
            case "addcatchparse":
                AddCmsCatchParse();
                break;
            default:
                break;
        }

    }

    #region 添加
    private void Add()
    {
        //添加权限
        ManageHelper.CheckAdminPower("system_catchadd");
        modelCmsCatch.CatchName = PageRequest.GetString("CatchName");
        if (modelCmsCatch.CatchName == "")
        {
            nsb.AddErr("请输入采集名称。");
        }
        modelCmsCatch.Url = PageRequest.GetString("Url");
        if (modelCmsCatch.Url == "")
        {
            nsb.AddErr("请输入抓取的地址。");
        }
        modelCmsCatch.StartPage = PageRequest.GetInt("StartPage", -1);
        if (modelCmsCatch.StartPage == -1)
        {
            nsb.AddErr("定位开头应为一个整数。");
        }
        modelCmsCatch.EndPage = PageRequest.GetInt("EndPage", -1);
        if (modelCmsCatch.EndPage == -1)
        {
            nsb.AddErr("定位结尾应为一个整数。");
        }

        modelCmsCatch.Interval = PageRequest.GetInt("Interval", -1);
        if (modelCmsCatch.Interval == -1)
        {
            nsb.AddErr("采集频率应为一个整数。");
        }

        modelCmsCatch.NodeID = PageRequest.GetInt("NodeID", -1);
        if (modelCmsCatch.NodeID == -1)
        {
            nsb.AddErr("请选择目标节点。");
        }
        modelCmsCatch.IsUsing = PageRequest.GetInt("IsUsing", 0);
        modelCmsCatch.SavePic = PageRequest.GetInt("SavePic", 0);
        modelCmsCatch.ContentType = PageRequest.GetInt("ContentType", 0);
        modelCmsCatch.ParseID = PageRequest.GetInt("ParseID", -1);
        if (modelCmsCatch.ParseID == -1)
        {
            nsb.AddErr("请选择采集解析");
        }
        modelCmsCatch.Whilelist = PageRequest.GetString("Whilelist");
        modelCmsCatch.Blacklist = PageRequest.GetString("Blacklist");
        modelCmsCatch.ContentPlus = PageRequest.GetString("ContentPlus");
        nsb.ChkErr();
        int retInt = bllCmsCatch.Add(modelCmsCatch);
        if (retInt == 1)
        {
            nsb.AddErr("已有相同的模型名称。");
        }
        else
        {   
            //添加日志
            ManageHelper.AddLog("system_catchadd", "添加了采集规则：" + modelCmsCatch.CatchName);
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"catchlist.aspx?sucmsg=添加成功。\";</script>");
        }
    }
    #endregion

    #region 反显修改页面
    private void Edit()
    {
        ActFlag = "editsave";
        ActName = "修改";
        int CatchID = PageRequest.GetInt("ID", -1);
        /*if (CatchID == -1)
        {
            nsb.AddErr("该采集规则不存在。");
            nsb.ChkErr();
        }*/
        modelCmsCatch = bllCmsCatch.GetModel(CatchID);
        if (modelCmsCatch==null)
        {
            nsb.AddErr("该采集规则不存在。");
            nsb.ChkErr();
        }
    }
    #endregion

    #region 修改时保存
    private void EditSave()
    {
        //修改权限
        ManageHelper.CheckAdminPower("system_catchupdate");
        modelCmsCatch.CatchID = PageRequest.GetInt("CatchID", -1);
        modelCmsCatch.CatchName = PageRequest.GetString("CatchName");
        if (modelCmsCatch.CatchName == "")
        {
            nsb.AddErr("请输入采集名称。");
        }
        modelCmsCatch.Url = PageRequest.GetString("Url");
        if (modelCmsCatch.Url == "")
        {
            nsb.AddErr("请输入抓取的地址。");
        }
        modelCmsCatch.StartPage = PageRequest.GetInt("StartPage", -1);
        if (modelCmsCatch.StartPage == -1)
        {
            nsb.AddErr("定位开头应为一个整数。");
        }
        modelCmsCatch.EndPage = PageRequest.GetInt("EndPage", -1);
        if (modelCmsCatch.EndPage == -1)
        {
            nsb.AddErr("定位结尾应为一个整数。");
        }

        modelCmsCatch.Interval = PageRequest.GetInt("Interval", -1);
        if (modelCmsCatch.Interval == -1)
        {
            nsb.AddErr("采集频率应为一个整数。");
        }

        modelCmsCatch.NodeID = PageRequest.GetInt("NodeID", -1);
        if (modelCmsCatch.NodeID == -1)
        {
            nsb.AddErr("请选择目标节点。");
        }
        modelCmsCatch.IsUsing = PageRequest.GetInt("IsUsing", 0);
        modelCmsCatch.SavePic = PageRequest.GetInt("SavePic", 0);
        modelCmsCatch.ContentType = PageRequest.GetInt("ContentType", 0);
        modelCmsCatch.ParseID = PageRequest.GetInt("ParseID", -1);
        if (modelCmsCatch.ParseID == -1)
        {
            nsb.AddErr("请选择采集解析");
        }
        modelCmsCatch.Whilelist = PageRequest.GetString("Whilelist");
        modelCmsCatch.Blacklist = PageRequest.GetString("Blacklist"); 
        modelCmsCatch.ContentPlus = PageRequest.GetString("ContentPlus");
        nsb.ChkErr();
        nsb.ChkErr();
        int RetFlag = bllCmsCatch.Update(modelCmsCatch);
        if (RetFlag == 1)
        {
            nsb.AddErr("对不起！已存在相同的内容模型。");
            nsb.ChkErr();
        }
        else
        {   
            //修改日志
            ManageHelper.AddLog("system_catchupdate", "修改了采集规则：" + modelCmsCatch.CatchName);
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"catchlist.aspx?sucmsg=修改成功。\";</script>");
        }
    }
    #endregion

    #region 删除
    private void Del()
    {
        //删除权限
        ManageHelper.CheckAdminPower("system_catchdelete");
        int ModelID = PageRequest.GetInt("ID", -1);
        if (ModelID < 0)//系统
        {
            nsb.AddErr("非法操作。");
            nsb.ChkErr();
        }
        bllCmsCatch.Delete(ModelID);
        ManageHelper.AddLog("system_catchdelete", "删除了采集规则，ID为：" + ModelID);
        Response.Redirect("catchlist.aspx?sucmsg=恭喜。删除成功。");
    }
    #endregion

    #region 添加
    private void AddCmsCatchParse()
    {
        //modelCmsCatchParse.ParseName = PageRequest.GetString("ParseName");
        //modelCmsCatchParse.ParseType = PageRequest.GetInt("ParseType", -1);
        //modelCmsCatchParse.ParseBegin = PageRequest.GetString("ParseBegin");
        //modelCmsCatchParse.ParseEnd = PageRequest.GetString("ParseEnd");
        //modelCmsCatchParse.ParseTags = PageRequest.GetString("ParseTags");
        //modelCmsCatchParse.ParseAttributes = PageRequest.GetString("ParseAttributes");
        //modelCmsCatchParse.ParseRule = PageRequest.GetString("ParseRule");
        //int Identtity = bllCmsCatchParse.Add(modelCmsCatchParse);
        //string selectName = "TextParseID";
        //if (modelCmsCatchParse.ParseType == 0)
        //{
        //    selectName = "ListParseID";
        //}
        //string RetStr = "<select name=\"" + selectName + "\" id=\"" + selectName + "\">";
        //RetStr += KeRui.BLL.Common.GetSelectList(Identtity, bllCmsCatchParse.GetSelectList(modelCmsCatchParse.ParseType));
        //RetStr += "</select>";
        //Response.Write(RetStr);
        //Response.End();
        Response.Write("该功能已删除"); Response.End();
    }
    #endregion

    #region 组建节点类表
    #region 栏目下拉列表
    public string SelectNodeList(int pId, int intTemp)
    {
        string RetStr = "";
        List<KeRui.Model.CmsNode> list = bllCmsNode.GetListByPIdandServerID(pId, ServerID);
        int j = 0;
        foreach (KeRui.Model.CmsNode model in list)
        {
            string StrStyle = "";
            j++;
            string Empty = "";
            for (int i = 0; i < model.Depth; i++)
            {
                Empty += "︱　";
            }
            if (model.ParentID == 0)
            {
                modelCmsSite = bllCmsSite.GetModel(model.SiteID);
                model.NodeName = modelCmsSite.SiteName;
                StrStyle = "style=\"background:#fff000;\"";
            }
            if (model.NodeID == intTemp)
            {
                RetStr += "<option value=" + model.NodeID + " selected "+StrStyle+">" + Empty + "{0}" + model.NodeName + "</option>";
            }
            else
            {
                RetStr += "<option value=" + model.NodeID + " "+StrStyle+">" + Empty + "{0}" + model.NodeName + "</option>";
            }
            if (model.ChildNum > 0)
            {
                RetStr += SelectNodeList(model.NodeID, intTemp);
            }
            if (j == list.Count)
            {
                RetStr = RetStr.Replace("{0}", "└");
            }
            else
            {
                RetStr = RetStr.Replace("{0}", "├");
            }
        }
        return RetStr.ToString();
    }
#endregion
    #endregion
}
